package com.dearbinge.data.service.impl.comment;

import com.baomidou.mybatisplus.plugins.Page;
import com.dearbinge.data.api.comment.MessageCommentService;
import com.dearbinge.data.dao.ShowFieldMapper;
import com.dearbinge.data.dao.ThumbsMapper;
import com.dearbinge.data.dao.comment.MessageCommentMapper;
import com.dearbinge.data.dao.comment.ReplyMapper;
import com.dearbinge.data.pojo.comment.MessageComment;
import com.dearbinge.data.pojo.comment.Reply;
import com.dearbinge.data.pojo.comment.vo.CommentReplyVO;
import com.dearbinge.data.pojo.comment.vo.CommentVO;
import com.dearbinge.data.pojo.comment.vo.ReplyVO;
import com.dearbinge.data.pojo.showField.ShowField;
import com.dearbinge.data.pojo.thumbs.Thumbs;
import com.dearbinge.utils.MapBeanUtil;
import com.dearbinge.utils.PageUtils;
import com.dearbinge.utils.enums.TypeCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 说明
 *
 * @author wes
 * @date $
 */
@Service("messageCommentService")
public class MessageCommentServiceImpl implements MessageCommentService {

    @Autowired
    private MessageCommentMapper messageCommentMapper;
    @Autowired
    private ReplyMapper replyMapper;
    @Autowired
    private ThumbsMapper thumbsMapper;
    @Autowired
    private ShowFieldMapper showFieldMapper;

    /**
     * 根据id和类型查询评论回复
     *
     * @param type     类型
     * @param entityId 对应信息表的id
     * @return 结果map
     */
    @Override
    public List<Object> getCommentReply(Integer type, String entityId, String userId) {
        //拿到评论和回复
        List<CommentReplyVO> commentReply = messageCommentMapper.listCommentReply(type, entityId);
        //该评论的点赞数 和 自己是否点赞
        List<Object> list = new ArrayList<Object>();
        for (CommentReplyVO commentReplyVO : commentReply) {
            Map<String, Object> map = MapBeanUtil.beanToMap(commentReplyVO);
            Boolean thumbs = null;
            Integer count = 0;
            if ("message_comment".equals(commentReplyVO.getTableName())) {
                Thumbs thumbsResult = thumbsMapper.getIsThumbs(commentReplyVO.getThisId(), 4, userId);
                //判断是否点赞
                if (thumbsResult != null) {
                    thumbs = !(thumbsResult.getIsDeleted());
                }
                //该评论的点赞数
                count = thumbsMapper.countThumbs(commentReplyVO.getThisId(), 4, userId);
            }
            map.put("thumbs", thumbs);
            map.put("thumbsCount", count);
            list.add(map);
        }
        return list;
    }


    /**
     * 获取评论和回复集
     *
     * @param type     类型
     * @param entityId 对应信息表的id
     * @param userId   用户id
     * @return 结果map
     */
    @Override
    public Page listCommentReply(Integer type, String entityId, String userId, Integer lastId, Integer current, Integer size) {
        current = PageUtils.getDefaultValues(current, 1);
        size = PageUtils.getDefaultValues(size, 10);
        Page<CommentVO> pageComment = new Page<CommentVO>(current, size);
        //总数
        Integer total = messageCommentMapper.countComment(type, entityId);
        pageComment.setTotal(total);
        //拿到评论
        List<CommentVO> commentReply = messageCommentMapper.listComment(type, entityId, lastId, pageComment);
        if (type.equals(TypeCodeEnum.COMMENT_SHOW.getCode())) {
            //秀场评论单独功能：遍历插入 回复集 该评论的点赞数 和 自己是否点赞
            for (CommentVO commentVO : commentReply) {
                String commentId = commentVO.getCommentId().toString();
                //判断是否点赞
                Thumbs thumbsResult = thumbsMapper.getIsThumbs(commentId, TypeCodeEnum.THUMBS_COMMENT.getCode(), userId);
                if (thumbsResult != null) {
                    commentVO.setThumbs(!thumbsResult.getIsDeleted());
                }
                //插入评论点赞数
                Integer count = thumbsMapper.countThumbs(commentId, TypeCodeEnum.THUMBS_COMMENT.getCode(), userId);
                commentVO.setThumbsCount(count);
                //插入回复集
                Page page = listReplyPage(commentId, null, 1, 1000);
                commentVO.setReplyResultList(page);
            }
        }
        pageComment.setRecords(commentReply);
        return pageComment;
    }

    /**
     * 获取回复集
     *
     * @param commentId 评论id
     * @param lastId    最新的id 用来防止分页的时候数据出现重复
     * @param current   当前页
     * @param size      每页显示的条数
     * @return 结果map
     */
    @Override
    public Page listReplyPage(String commentId, Integer lastId, Integer current, Integer size) {
        current = PageUtils.getDefaultValues(current, 1);
        size = PageUtils.getDefaultValues(size, 10);
        Page<ReplyVO> page = new Page<ReplyVO>(current, size);
        Integer total = replyMapper.countReply(commentId);
        page.setTotal(total);
        List<ReplyVO> listReply = replyMapper.listReply(commentId, lastId, page);
        page.setRecords(listReply);
        return page;
    }


    /**
     * 增加评论
     *
     * @param messageComment 评论
     * @return 结果map
     */
    @Override
    public Integer insertMessageComment(MessageComment messageComment) {
        Integer comment = messageCommentMapper.insertMessageComment(messageComment);
        //修改秀场表的评论次数
        try {
            if (comment > 0 && messageComment.getReplyType().equals(TypeCodeEnum.COMMENT_SHOW.getCode())) {
                ShowField showField = showFieldMapper.getById(Integer.valueOf(messageComment.getEntityId()));
                if (showField != null) {
                    Integer messageCount = showField.getMessageCount();
                    messageCount++;
                    showField.setMessageCount(messageCount);
                    showFieldMapper.update(showField);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return comment;
    }

    /**
     * 修改评论
     *
     * @param messageComment id
     * @return 结果
     */
    @Override
    public Integer updateMessageComment(MessageComment messageComment) {
        Integer comment = messageCommentMapper.updateMessageComment(messageComment);
        return comment;
    }

    /**
     * 删除评论
     *
     * @param commentId 评论表ID（不用类型的表的主ID）
     * @param tableId   表ID（不用类型的表的主ID）
     * @param tableType 对应的表类型（1：生活  2：商品  3：秀场）
     * @return 结果提示
     */
    @Override
    public Integer deleteMessageComment(Integer commentId, Integer tableType, Integer tableId) {
        Integer comment = messageCommentMapper.deleteMessageComment(commentId);
        if (comment > 0 && tableType.equals(TypeCodeEnum.COMMENT_SHOW.getCode())) {
            //修改秀场的评论数
            ShowField showField = showFieldMapper.getById(tableId);
            if (showField != null) {
                Integer messageCount = showField.getMessageCount();
                messageCount--;
                showField.setMessageCount(messageCount);
                showFieldMapper.update(showField);
            }
        }
        return comment;
    }

    /**
     * 增加回复
     *
     * @param reply 回复表
     * @return 回复表id
     */
    @Override
    public Integer insertReply(Reply reply) {
        Integer integer = replyMapper.insertReply(reply);
        return integer;
    }

    /**
     * 修改回复
     *
     * @param reply reply
     * @return 结果
     */
    @Override
    public Integer updateReply(Reply reply) {
        Integer result = replyMapper.updateReply(reply);
        return result;
    }

    /**
     * 删除回复
     *
     * @param id 主键id
     * @return 结果提示
     */
    @Override
    public Integer deleteReply(Integer id) {
        return replyMapper.deleteReply(id);
    }

}
